# coding=UTF-8
import os
import sys

sys.path.append(os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + "."))
from utils import k_util, json_util
import decimal


# 判断突破均线情况
def judge_form(code, rt_price):
    day_k_list = k_util.day_k_local(code, 120)
    ave_dict = {'5': 0, '10': 0, '20': 0, '30': 0, '60': 0}
    length = len(day_k_list)
    for key in ave_dict:
        if length < int(key):
            continue
        ave_dict[key] = ave_line_price(day_k_list, int(key))
    for key in ave_dict:
        if rt_price >= ave_dict[key]:
            ave_dict[key] = '+:' + str(key)
        elif ave_dict[key] == 0:
            ave_dict[key] = '' + str(key)
        else:
            ave_dict[key] = '-:' + str(key)
    return ave_dict


# 获取平均线
# 5日均线（MA5）=最近连续5个交易日收盘价之和÷5，即日之前的5日均线数值均会在行情上方显示。
def get_ave_line(code, cal_days, cal_count, klines):
    print("获取平均线,股票编码" + code)
    # print(klines)
    result = {}
    new_klines = []
    sp_list = []
    kl_len = len(klines)
    if kl_len < cal_days + cal_count:
        return result
    for i in range(cal_days + cal_count):
        new_klines.append(klines[kl_len - 1 - i])
    for s_info in new_klines:
        amount = decimal.Decimal(s_info.split(',')[2])
        sp_list.append(amount)
    # 最近连续5个交易日收盘价之和
    sum_amount = 0
    for i in range(cal_count):
        if i == 0:
            for j in range(cal_days):
                sum_amount += sp_list[i + j]
        else:
            sum_amount = (sum_amount - sp_list[i - 1] + sp_list[i + cal_days - 1])
        result[new_klines[i].split(',')[0]] = round(sum_amount / cal_days, 2)
    return result


# 5日均线（MA5）=最近连续5个交易日收盘价之和÷5，即日之前的5日均线数值均会在行情上方显示。
def five_ave_line_price(code, klines):
    # result = {'2020-06-10','62.08'}
    result = get_ave_line(code, 5, 1, klines)
    for key in result:
        return float(result[key])


# n日均线
def ave_line_price_n(code, klines, n):
    # result = {'2020-06-10','62.08'}
    result = get_ave_line(code, n, 1, klines)
    for key in result:
        return float(result[key])


# 传入的均线价格 + 当前实时价格
# klines 包含rt_price当天k
def ave_price_rt(klines, rt_price):
    k_len = len(klines)
    sum = rt_price
    for i in range(1, k_len):
        sum += float(json_util.info_to_json(klines[i])['sp'])
    return round(sum / k_len, 2)


# n日均线价格
def ave_line_price(k_line_dict, n):
    total_price = 0
    for i in range(n):
        total_price += float(json_util.info_to_json(k_line_dict[i])['sp'])
    return round(total_price / n, 2)


def five_ave_line_k(code):
    # result = {'2020-06-10','62.08'}
    return get_ave_line(code, 5, 1, k_util.get_day_k(code))


# 获取平均线
# local_kline
def local_ave_line(code, days, count, local_kline):
    print("获取平均线,股票编码" + code)
    sum_amount = 0
    dividend = days
    for stock in local_kline:
        if count == 0:
            if days == 0:
                break
            sum_amount += float(eval(str(stock))['sp'])
            days -= 1
        else:
            count -= 1
    return round(sum_amount / dividend, 2)


def ave_line_with_local(code, days, count):
    local_kline = k_util.day_k_local(code, days + count + 1)
    return local_ave_line(code, days, count, local_kline)


if __name__ == '__main__':
    print('junxian')
    # klines = k_util.get_day_k('601999')
    # get_ave_line('601999', 5, 5, klines)
    a = ave_line_with_local('000008', 2, 0)
    print(a)
